package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

import xtom.frame.util.XtomToastUtil;

/**
 * 支付宝账户新增和编辑界面
 */
public class AlipayAccountEditActivity extends BaseActivity {
	
	private final static int REQUEST_TIXIAN = 0x02;//提现
	
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private EditText editText;
	private Button button;
	
	private String aliuser;
	
	private boolean needtoCashAdd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_cardinput);
		super.onCreate(savedInstanceState);
        User user = getApplicationContext().getUser();
        if (user != null) {
            String aliAccount = BaseUtil.transNull(user.getAlipay());
            editText.setText(aliAccount);
			if (!isNull(aliAccount))
                editText.setSelection(user.getAlipay().length());
        }
	}

	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ALI_SAVE:
			showProgressDialog("请稍候");
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ALI_SAVE:
			cancelProgressDialog();
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ALI_SAVE:
			User user = getApplicationContext().getUser();
			user.setAlipay(this.aliuser);
			if (needtoCashAdd) {// 如果是首次设置银行卡，跳转至提现页面
				Intent it = new Intent(mContext, Cash2AlipayActivity.class);
				startActivityForResult(it, REQUEST_TIXIAN);
			}
            finish();
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ALI_SAVE:
			XtomToastUtil.showShortToast(mContext, "保存失败,"+baseResult.getMsg());
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ALI_SAVE:
			XtomToastUtil.showShortToast(mContext, "保存失败，请稍候再试");
			break;
		}
	}

	@Override
	protected void findView() {
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRight = (Button) findViewById(R.id.title_right_btn);

		editText = (EditText) findViewById(R.id.edittext);
		button = (Button) findViewById(R.id.button);
	}

	@Override
	protected void getExras() {
		needtoCashAdd = mIntent.getBooleanExtra("needtoCashAdd", false);
	}

	@Override
	protected void setListener() {
		titleText.setText("支付宝账户");
		titleLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleRight.setVisibility(View.VISIBLE);
		titleRight.setText("保存");
		button.setVisibility(View.GONE);
		titleRight.setOnClickListener(new SaveListener());
		editText.setHint("请输入支付宝账户");
	}
	private class SaveListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			aliuser = editText.getText().toString();
			if (isNull(aliuser)) {
				XtomToastUtil.showShortToast(mContext, "请输入支付宝账户");
				return;
			}
			String token = getApplicationContext().getUser().getToken();
			getNetWorker().aliSave(token,aliuser);
		}
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case REQUEST_TIXIAN:
			setResult(RESULT_OK);
			finish();
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
